package com.epam.training;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.epam.training.domain.Bonus;
import com.epam.training.service.bonus.BonusService;

/**
 * @author Bogdan
 *
 */

@Controller
public class AddBonusController {
	private final BonusService service;

	@Autowired
	public AddBonusController(BonusService service) {
		this.service = service;
	}

	@ModelAttribute
	private Bonus createBonus(@RequestParam BigDecimal bAmount,
			@RequestParam String bName, @RequestParam String bDescription) {
		Bonus bonus = new Bonus();
		bonus.setAmount(bAmount);
		bonus.setName(bName);
		bonus.setDescription(bDescription);

		return bonus;
	}

	@RequestMapping(value = "/admin/addBonus", method = RequestMethod.POST)
	private String addNewBonus(@ModelAttribute Bonus bonus, Model model) {
		service.save(bonus);

		model.addAttribute("message", "bonus.added");

		return AdminPageController.ADMIN_PAGE;
	}

	@RequestMapping(value = "/admin/updateBonus", method = RequestMethod.POST)
	private String updateBonus(@ModelAttribute Bonus bonus,
			@RequestParam Integer bonusId, Model model) {
		service.update(bonus, bonusId);
		model.addAttribute("message", "bonus.update");

		return AdminPageController.ADMIN_PAGE;
	}

}
